#!/bin/bash

# AnyPay launchd服务管理脚本

PLIST_FILE="com.anypay.service.plist"
PLIST_SOURCE="$(pwd)/tools/scripts/$PLIST_FILE"
PLIST_DEST="/Library/LaunchDaemons/$PLIST_FILE"
USER_PLIST_DEST="$HOME/Library/LaunchAgents/$PLIST_FILE"

# 颜色输出
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

log() {
    echo -e "${GREEN}[INFO]${NC} $1"
}

warn() {
    echo -e "${YELLOW}[WARN]${NC} $1"
}

error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 安装系统级服务（需要sudo）
install_system_service() {
    log "安装系统级launchd服务..."

    if [ ! -f "$PLIST_SOURCE" ]; then
        error "找不到plist文件: $PLIST_SOURCE"
        return 1
    fi

    # 停止现有服务
    sudo launchctl unload "$PLIST_DEST" 2>/dev/null || true

    # 复制plist文件
    sudo cp "$PLIST_SOURCE" "$PLIST_DEST"
    sudo chown root:wheel "$PLIST_DEST"
    sudo chmod 644 "$PLIST_DEST"

    # 加载服务
    sudo launchctl load "$PLIST_DEST"

    log "系统级服务安装完成"
}

# 安装用户级服务（不需要sudo）
install_user_service() {
    log "安装用户级launchd服务..."

    if [ ! -f "$PLIST_SOURCE" ]; then
        error "找不到plist文件: $PLIST_SOURCE"
        return 1
    fi

    # 创建目录
    mkdir -p "$HOME/Library/LaunchAgents"

    # 停止现有服务
    launchctl unload "$USER_PLIST_DEST" 2>/dev/null || true

    # 复制plist文件
    cp "$PLIST_SOURCE" "$USER_PLIST_DEST"

    # 加载服务
    launchctl load "$USER_PLIST_DEST"

    log "用户级服务安装完成"
}

# 卸载服务
uninstall_service() {
    log "卸载launchd服务..."

    # 卸载系统级服务
    if [ -f "$PLIST_DEST" ]; then
        sudo launchctl unload "$PLIST_DEST" 2>/dev/null || true
        sudo rm -f "$PLIST_DEST"
        log "系统级服务已卸载"
    fi

    # 卸载用户级服务
    if [ -f "$USER_PLIST_DEST" ]; then
        launchctl unload "$USER_PLIST_DEST" 2>/dev/null || true
        rm -f "$USER_PLIST_DEST"
        log "用户级服务已卸载"
    fi
}

# 查看服务状态
status_service() {
    log "检查服务状态..."

    echo "=== 系统级服务状态 ==="
    if sudo launchctl list | grep -q com.anypay.service; then
        sudo launchctl list com.anypay.service
    else
        warn "系统级服务未运行"
    fi

    echo "=== 用户级服务状态 ==="
    if launchctl list | grep -q com.anypay.service; then
        launchctl list com.anypay.service
    else
        warn "用户级服务未运行"
    fi

    echo "=== 进程状态 ==="
    ps aux | grep anypay | grep -v grep || warn "未找到anypay进程"
}

# 重启服务
restart_service() {
    log "重启launchd服务..."

    # 重启系统级服务
    if [ -f "$PLIST_DEST" ]; then
        sudo launchctl unload "$PLIST_DEST" 2>/dev/null || true
        sleep 2
        sudo launchctl load "$PLIST_DEST"
        log "系统级服务已重启"
    fi

    # 重启用户级服务
    if [ -f "$USER_PLIST_DEST" ]; then
        launchctl unload "$USER_PLIST_DEST" 2>/dev/null || true
        sleep 2
        launchctl load "$USER_PLIST_DEST"
        log "用户级服务已重启"
    fi
}

# 显示帮助
show_help() {
    echo "AnyPay launchd服务管理脚本"
    echo
    echo "用法: $0 [命令]"
    echo
    echo "命令:"
    echo "  install-system    安装系统级服务（需要sudo）"
    echo "  install-user      安装用户级服务"
    echo "  uninstall         卸载所有服务"
    echo "  restart           重启服务"
    echo "  status            查看服务状态"
    echo "  help              显示此帮助"
    echo
    echo "推荐使用用户级服务，不需要sudo权限"
}

# 主函数
case "${1:-help}" in
    install-system)
        install_system_service
        ;;
    install-user)
        install_user_service
        ;;
    uninstall)
        uninstall_service
        ;;
    restart)
        restart_service
        ;;
    status)
        status_service
        ;;
    help|--help|-h)
        show_help
        ;;
    *)
        error "未知命令: $1"
        show_help
        exit 1
        ;;
esac